<?php
/********************************************************************
 *	Author:
 *		Jestery Liu <jestery@gmail.com>
 *		http://www.pte.cn
 *
 *	File:			StringHelper.class.php
 *	Create Date:	2007-05-12 11:39:52 CST
 *******************************************************************/

class StringHelper
{
	private function __construct() {}

	public static function strEndsWith($str, $needle)
	{
		if ( ($nl=strlen($needle)) > ($sl=strlen($str)) )
			return false;
		if ( ($needle == $str) && !empty($needle) )
			return true;
		return strpos($str, $needle, $sl-$nl)!==false;
	}

	public static function strStartsWith($str, $needle)
	{
		if ( ($len = strlen($needle)) > strlen($str) )
			return false;
		if ( ($needle == $str) && !empty($needle) )
			return true;
		return strncmp($str, $needle, $len)==0;
	}
	
	public static function getRandomString($length, $numeric = 0)
	{
		if($numeric)
		{
			$hash = sprintf('%0'.$length.'d', mt_rand(0, pow(10, $length) - 1));
		}
		else
		{
			$hash = '';
			$chars = 'ABCHIJghijklmnopqrs456DEFG789tuvwKLMNOPQRSXYZ0123abcdefTUVWxyz';
			$max = strlen($chars) - 1;
			for($i = 0; $i < $length; $i++)
			{
				$hash .= $chars[mt_rand(0, $max)];
			}
		}
		return $hash;
	}
}

/* vim: set ts=4 sw=4 ff=unix: */
